//
//  MSLoginBaseViewController.m
//  MSJD
//
//  Created by xiangbo on 2018/9/27.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSLoginBaseViewController.h"

@interface MSLoginBaseViewController ()

@property (nonatomic, strong) UIButton *backButton;

@end

@implementation MSLoginBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseView = [[UIView alloc] init];
    [self.view addSubview:self.baseView];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 20, 44, 44);
//    [backButton setTitle:@"<" forState:UIControlStateNormal];
//    [backButton setTitleColor:MSThemeColor forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    self.backButton = backButton;
//    [self addKeyboardObservers];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addKeyboardObservers];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self removeKeyboardNotifications];
    [self.view endEditing:YES];
}

- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)addKeyboardObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
/** 移除监听 */
- (void)removeKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    
}

- (void)keyboardWillHidden:(NSNotification *)notification {
    self.baseView.center = self.view.center;
}

#pragma mark - Public Method

- (void)hiddenBackButton {
    self.backButton.hidden = YES;
}

#pragma mark - System Method

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    // 点击空白处隐藏键盘
    [self.view endEditing:YES];
}

@end
